﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace CH.Easy.OpenLogin
{
    public class SinaWeiboOauthV2 : AbstractOauthV2
    {
        public SinaWeiboOauthV2()
        {
            this.InitConfig();
        }
        public SinaWeiboOauthV2(string callBack)
        {
            this.InitConfig();
            base.CallBack = callBack;
        }
        public SinaWeiboOauthV2(string accessToken, string openID)
        {
            this.InitConfig();
            base.AccessToken = accessToken;
            base.OpenID = openID;
        }
        public SinaWeiboOauthV2(string accessToken, string openID, string callBack)
        {
            this.InitConfig();
            base.AccessToken = accessToken;
            base.OpenID = openID;
            base.CallBack = callBack;
        }


        private void InitConfig()
        {
            base.OpenType = OpenType.SinaWeibo;
            base.AuthorizeUrl = "https://api.weibo.com/oauth2/authorize";
            base.TokenUrl = "https://api.weibo.com/oauth2/access_token";
            base.MeUrl = "";
            base.CallBack = Common.GetConfig("CallBackUrl") + "/common/open/callback.aspx?type=" + (int)base.OpenType;
            base.AppKey = Common.GetConfig("SinaWeiboAppKey");
            base.AppSecret = Common.GetConfig("SinaWeiboAppSecret");
            base.OpenRequest = new OpenRequest();
        }

        public override string GetAuthorizeUrl()
        {
            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"response_type","code"},
                {"client_id",base.AppKey},
                {"redirect_uri",base.CallBack},
                {"display","default"},
                {"state",Guid.NewGuid().ToString().Replace("-","").ToLower()}

            };

            return base.AuthorizeUrl + "?" + param.GetQueryString();
        }
        public override AccessTokenDTO GetAccessToken(string code)
        {
            AccessTokenDTO accessTokenDTO = null;
            if (string.IsNullOrEmpty(code))
                return accessTokenDTO;


            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"grant_type","authorization_code"},
                {"client_id",base.AppKey},
                {"client_secret",base.AppSecret},
                {"redirect_uri",base.CallBack},
                {"code",code}

            };

            try
            {
                string result = base.OpenRequest.HttpPost(base.TokenUrl, param.GetQueryString());
                if (result.Contains("=") && result.Contains("&"))
                {
                    Dictionary<string, string> list = result.GetParam();
                    accessTokenDTO = new AccessTokenDTO();
                    accessTokenDTO.access_token = list["access_token"];
                    accessTokenDTO.expires_in = list["expires_in"];
                    accessTokenDTO.uid = list["uid"];
                }
                else
                {
                    //如果返回来的数据是json 格式
                    result = result.Replace("callback(", "").Replace(");", "");
                    accessTokenDTO = JsonConvert.DeserializeObject<AccessTokenDTO>(result);
                }
                //注意 新浪微博在这一步的时候就已经返回这个OpenID了 
                base.OpenID = accessTokenDTO.uid;
            }
            catch
            {

                return null;
            }

            return accessTokenDTO;
        }




    }
}
